class_name CardUI extends Control

signal reparent_request(which_card_ui: CardUI)

@export var player_modifiers: ModifierHandler
@export var card: Card: set = _set_card
@export var char_stats: CharacterStats: set = _set_char_stats

@onready var card_visuals: CardVisuals = %CardVisuals
@onready var drop_point_dector: Area2D = %DropPointDector
@onready var card_state_machine: CardStateMachine = %CardStateMachine
@onready var targets: Array[Node] = []


var parent: Control
var tween: Tween
var playable := true : set = _set_playable
var disable := false
var original_index := 0

func _ready() -> void:
	card_state_machine.init(self)
	Events.card_aim_started.connect(_card_aim_or_drag_started)
	Events.card_drag_started.connect(_card_aim_or_drag_started)
	Events.card_aim_ended.connect(_card_aim_or_drag_ended)
	Events.card_drag_ended.connect(_card_aim_or_drag_ended)

func animate_to_position(new_position: Vector2, duration: float) -> void:
	tween = create_tween().set_trans(Tween.TRANS_CIRC).set_ease(Tween.EASE_OUT)
	tween.tween_property(self, "global_position", new_position, duration)
	
func do_reparent(card_ui: CardUI) -> void:
	reparent_request.emit(card_ui)
	
func play() -> void:
	if not card:
		return
	card.play(targets, char_stats, player_modifiers)
	Events.do_tooltip_hide(true)
	queue_free()
	
func request_tooltip() -> void:
	var enemy_modifiers := _get_active_enemy_modifiers()
	var tooltip := card.get_update_tooltip(player_modifiers, enemy_modifiers)
	Events.do_tooltip_show(card.icon, tooltip)

	
func _get_active_enemy_modifiers() -> ModifierHandler:
	if targets.is_empty() or targets.size() > 1 or not targets[0] is Enemy:
		return null
	return (targets[0] as Enemy).modifier_handler
	
func _set_card(value: Card) -> void:
	if not is_node_ready():
		await ready
	card = value
	card_visuals.card = card
	
func _set_playable(value: bool) -> void:
	playable = value
	if not playable:
		card_visuals.cost.add_theme_color_override("font_color", Color.RED)
		card_visuals.icon.modulate = Color(1,1,1,0.5)
	else:
		card_visuals.cost.remove_theme_color_override("font_color")
		card_visuals.icon.modulate = Color(1,1,1,1)
	
func _set_char_stats(value: CharacterStats) -> void:
	char_stats = value
	char_stats.stats_changed.connect(_card_stats_changed)
	
func _card_aim_or_drag_started(value: CardUI) -> void:
	if value == self:
		return
	disable = true
	
func _card_aim_or_drag_ended(_value: CardUI) -> void:
	disable = false
	playable = char_stats.can_play_card(card)
	
func _card_stats_changed() -> void:
	playable = char_stats.can_play_card(card)
	
func _input(event: InputEvent) -> void:
	card_state_machine.on_input(event)
	
func _on_gui_input(event: InputEvent) -> void:
	card_state_machine.on_gui_input(event)
	
func _on_mouse_entered() -> void:
	card_state_machine.on_mouse_entered()
	
func _on_mouse_exited() -> void:
	card_state_machine.on_mouse_exited()

func _on_drop_point_dector_area_entered(area: Area2D) -> void:
	if not targets.has(area):
		targets.append(area)

func _on_drop_point_dector_area_exited(area: Area2D) -> void:
	targets.erase(area)
